from django.shortcuts import render_to_response

from .models import InterfaceList
from .models import InterfaceTestCase
from .models import InterfaceTestSuite
from .models import SuitCase
from django.http import HttpResponseRedirect
from django.core.exceptions import ObjectDoesNotExist
import json

# from myApp.models import DataSrc


   


def getsuitelist(object):
    pass


def getinterfacelist(object):
    pass


def getcaselist(object):
    pass


# 通过suite获取case列表
def suitegetcase(suiteid):
    """
    根据id查询用例列表，输入为整数
    :param suiteid:传ID，整数
    :return:
    """
    case_list = []
    try:
        testsuite = InterfaceTestSuite.objects.get(id=suiteid)
    except ObjectDoesNotExist:
        print("Either the InterfaceTestSuite  doesn't exist.")
    suitcase = testsuite.suite_case.all().values()
    if len(suitcase) > 0:
        print('*********suitcase:', suitcase)
        for case in suitcase:
            run_case = {}
            print('case:', case)
            interface_id = case['interface_id']
            interface = InterfaceList.objects.filter(id=interface_id).values()[0]
            print('interface:', interface)
            run_case['suite'] = suiteid
            run_case['run_order'] = case['id']
            run_case['interface_id'] = interface['id']
            run_case['interface_url'] = interface['interface_url']
            run_case['interface_header'] = interface['interface_header']
            run_case['interface_protocol'] = interface['interface_protocol']
            run_case['interface_method'] = interface['interface_method']
            run_case['interface_body_form'] = interface['interface_body_form']
            run_case['case_id'] = case['id']
            run_case['case_desc'] = case['case_desc']
            run_case['case_data'] = case['case_data']
            run_case['case_expected'] = case['case_expected']
            case_list.append(run_case)
    return case_list


def createruncase(caselist):
    """
    将输入的待执行的用例列表插入到数据库中
    :param caselist: [{},{}]
    :return:
    """
    # SuitCase.objects.bulk_create(caselist)
    if len(caselist) > 0:
        for case in caselist:
            suiteid = InterfaceTestSuite.objects.get(id=case['suite'])
            interface_id = InterfaceList.objects.get(id=case['interface_id'])
            case_id = InterfaceTestCase.objects.get(id=case['case_id'])
            SuitCase.objects.update_or_create(
                interface_id = interface_id, case_id = case_id,
                defaults={'suite':suiteid, 'run_order':case['run_order'],
                'interface_host_name':suiteid.suite_host_name,
                'interface_url':case['interface_url'],
                'interface_header':case['interface_header'], 'interface_protocol':case['interface_protocol'],
                'interface_method':case['interface_method'], 'interface_body_form':case['interface_body_form'],
                'case_desc':case['case_desc'], 'case_data':case['case_data'],
                'case_expected':case['case_expected']})
        return 'sucess'
    else:
        return "false:caselist为空，不做任何操作"


def run_case(suiteIDlist):
    if len(suiteIDlist) > 0:
        for suiteID in suiteIDlist:
            caselist = SuitCase.objects.filter(suite=suiteID).values()
            print('===============caselist=============:', list(caselist))


